﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Globalization;
using System.Resources;
using System.Threading;

namespace CRMG.Container.Views.Payments
{
    public partial class FrmPayment : Telerik.WinControls.UI.RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        PaymentManager paymentMan;
        StudentManager stdntMan;
        int _studentId = -1;

        public FrmPayment()
            : this(-1)
        { }

        public FrmPayment(int studentId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmPayment).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmPayment).Assembly);
                paymentMan = new PaymentManager();
                stdntMan = new StudentManager();
                this.Text = formRes.GetString("FrmPayment.Caption");
                this.lblStudent.Text = formRes.GetString("FrmPayment.lblStudent");
                this.lblAmount.Text = formRes.GetString("FrmPayment.lblAmount");
                this.lblPaymentTime.Text = formRes.GetString("FrmPayment.lblPaymentTime");
                this.lblPaymentType.Text = formRes.GetString("FrmPayment.lblPaymentType");
                this.lblBillType.Text = formRes.GetString("FrmPayment.lblBillType");
                btnSave.Text = formRes.GetString("Save");
                btnCancel.Text = formRes.GetString("Cancel");
                _studentId = studentId;
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2061,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2061)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPayment.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void FrmPaymentLoad(object sender, EventArgs e)
        {
            try
            {
                dtpPaymentTime.Culture = Thread.CurrentThread.CurrentUICulture;
                ControlUtil.LoadDataTable2DropDownList(drpStudent, stdntMan.GetActiveStudentList(), "Name", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpPaymentType, paymentMan.GetPaymentTypes(), "PaymentTypeName", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpBillType, paymentMan.GetBillTypes(), "BillTypeName", "OBJID");
                drpPaymentType.SelectedIndex = 0;
                drpBillType.SelectedIndex = 0;
                if (_studentId != -1)
                {
                    drpStudent.SelectedValue = _studentId;
                    drpStudent.Enabled = false;
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPayment.Load.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2062,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2062)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPayment.Load.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                if (drpStudent.SelectedValue.ToInt() < 1)
                {
                    ControlUtil.ShowWarningMessage(
                        messageRes.GetString("FrmPayment.Save.NotSelectedStudent"),
                        messageRes.GetString("Warning"));
                    return;
                }
                else if (txtAmount.NumericValue == 0)
                {
                    ControlUtil.ShowWarningMessage(
                        messageRes.GetString("FrmPayment.Save.ZeroAmount"),
                        messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    InPayment inPay = new InPayment()
                    {
                        UserId = AppVariables.CurrentUser.OBJID,
                        StudentId = _studentId == -1 ? drpStudent.SelectedValue.ToInt() : _studentId,
                        Amount = (float)txtAmount.NumericValue,
                        PaymentType = drpPaymentType.SelectedValue.ToInt(),
                        PaymentTime = dtpPaymentTime.Value,
                        BillType = drpBillType.SelectedValue.ToInt()
                    };
                    int Id = paymentMan.AddPayment(inPay);
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr(),
                        LogObject = Id,
                        TransactionType = TransactionTypes.Insert
                    };
                    transLogger.Insert();
                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                        messageRes.GetString("FrmPayment.Save.Success"),
                        messageRes.GetString("Info"));
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPayment.Save.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2063,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2063)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPayment.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
